<template>
  <div class="app-container">
    <div class="query-panel">
      <div class="query-panel-filter">
        <div class="query-form">
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
            <el-form-item label="内容" prop="content">
              <el-input v-model="queryParams.content" placeholder="请输入内容" clearable size="small"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="query-search">
        <div class="query-search-btn">
          <div class="query-search-btn">
            <el-button class="c-btn-reset" icon="el-icon-query-reset" size="mini" @click="resetQuery">重置</el-button>
            <el-button class="c-btn-search" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </div>
        </div>
      </div>
    </div>

    <el-table v-loading="loading" :data="messageList" stripe>
      <el-table-column label="标题" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="scope.row.isread == 1">{{ scope.row.title }}</span>
          <span v-else style="color: #5fb87c">{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="scope.row.isread == 1">{{ scope.row.content }}</span>
          <span v-else style="color: #5fb87c">{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否处理" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.isread == 1">已处理</span>
          <span v-else style="color: #5fb87c">未处理</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" class="r-item-btn" icon="el-icon-c-view" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" disabled="true">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel-btn" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMessage, getMessage, updateMessage } from "@/api/basic/message";

export default {
  name: "Station",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      messageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        sortfield: "isread",
        sorttype: "asc",
        pageNum: 1,
        pageSize: 10,
        content: null,
        receiver: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      var user = this.$store.getters.user;
      if (user != null) {
        this.queryParams.receiver = user.id;
      }
      listMessage(this.queryParams).then((response) => {
        this.messageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        remark: null,
        createts: null,
        createdby: null,
        companyid: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids;
      getMessage(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看消息";
        updateMessage({ id: id, isread: 1 }).then((response) => {
          this.getList();
        })

        this.$nextTick(() => {
          this.dialogIcon("view");
        });
      });
    },
  },
};
</script>
